package com.nc.shop.service.controller;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import com.alibaba.fastjson.JSONObject;
import com.plug.result.RetResponse;
import com.plug.result.RetResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@RestController
@RequestMapping("v1/index")
@Api
public class IndexController {
    @Value("${folderPath}")
    private String folderPath;

    @ApiOperation("保存首页配置")
    @PostMapping
    public RetResult save(@RequestBody JSONObject index){
        FileWriter writer = new FileWriter(folderPath+"/index.json");
        writer.write(index.toJSONString());
        return RetResponse.makeOKRsp();
    }

    @ApiOperation("获得首页配置")
    @GetMapping
    public RetResult findAll(){
        FileReader fileReader = new FileReader(folderPath+"/index.json");
        String result = fileReader.readString();
        return RetResponse.makeOKRsp(result);
    }
}
